define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Productmanager_sale_edit, $timeout, Auth, GoCatTypeAuth, $ionicSideMenuDelegate, GoPage, GoDialog) {
		
		$scope.$on('$ionicView.afterEnter', function() {
			//得到当前用户身份。1:开发商，2:品牌代理商，3:代理商/分仓	4:经销商
			$scope.user_type = Auth.check_user_type();
		}, false);
		
		$scope.input = new Object();
		$scope.input.key = "";
		$scope.filterString = "";
		//打开侧边的筛选菜单
		$scope.openRightMenu = function() {
			$ionicSideMenuDelegate.toggleRight();
		}
		//关闭侧边的筛选菜单
		$scope.closeRightMenu = function(){
			if($ionicSideMenuDelegate.isOpenRight()){
				$ionicSideMenuDelegate.toggleRight();
			}
		}
		
		//价格排序
		$scope.order = 1; //默认为1
		$scope.sortByPrice = function() {
			if($scope.order == 2) {
				$scope.order = 3;
			} else {
				$scope.order = 2;
			}
			Productmanager_sale_edit.goods_search($scope.input.key, 1, 10, $scope.order, $scope.filterString, true,function(data) {
				$scope.goods = data.goods;
			});
			$scope.closeRightMenu();
		}
		//销量排序
		$scope.sortByCount = function(order) {
			if($scope.order == 4) {
				$scope.order = 5;
			} else {
				$scope.order = 4;
			}
			Productmanager_sale_edit.goods_search($scope.input.key, 1, 10, $scope.order, $scope.filterString, true,function(data){
				$scope.goods = data.goods;
			});
			$scope.closeRightMenu();
		};
		
		//全选、反选
		var checkBox = document.getElementsByClassName("checkbox_goodid");
		$scope.getCheckBox = function() {
			for(var i = 0; i < checkBox.length; i++) {
				if($scope.productModel.is_all) {
					$scope.productModel.danModel[i] = true;
				} else {
					$scope.productModel.danModel[i] = false;
				}
			}
		}
		$scope.checkEven = function(){
			for(var i = 0; i < checkBox.length; i++) {
				if(!$scope.productModel.danModel[i]) {
					$scope.productModel.is_all = false;
					break;
				} else {
					$scope.productModel.is_all = true;
				}
			}
		}
		//获取复选框的值
		$scope.getCheckBoxValue = function() {
			var good_ids = '';
			for(var i = 0; i < checkBox.length; i++) {
				if(checkBox[i].checked) {
					good_ids += checkBox[i].value + ",";
				}
			}
			//去掉最后一个逗号
			if(good_ids.length > 1) {
				good_ids = good_ids.substr(0, good_ids.length - 1);
			}
			return good_ids;
		}

		//删除商品(有权限)
		$scope.delete_goods = function() {
			var good_ids = $scope.getCheckBoxValue();//得到一串id
			if(!good_ids){
				GoDialog.toast("请先选择要删除的商品！");
				return;
			}
			Productmanager_sale_edit.delete_goods(good_ids, function(data) {
				if(data.success) {
					GoDialog.toast("删除成功！");
					$scope.init_item();
				} else {
					GoDialog.toast(data.msg);
				}
			});
		}
		//删除商品（没有权限）
		$scope.delete_goods_noRight = function() {
			GoDialog.toast("对不起，您没有权限删除商品！");
		}
		//下架商品
		$scope.update_goods = function() {
			var good_ids = $scope.getCheckBoxValue();
			if(!good_ids){
				GoDialog.toast("请先选择要下架的商品！");
				return;
			}
			Productmanager_sale_edit.update_goods(good_ids, false, function(data) {
				if(data.success) {
					GoDialog.toast("操作成功！");
					$scope.init_item();
				}else{
					GoDialog.toast(data.msg);
				}
			});
		}
		//删除现有数据数组中的一个元素--公用--“上下架”、“删除商品”共用
		$scope.init_item = function(){
			$scope.init_data();
			for(var j = 0; j < $scope.productModel.danModel.length; j++){
				$scope.productModel.danModel[j] = false;		//初始化复选框
			}
			$scope.productModel.is_all = false;
		}
		
//		if($scope.good == undefined) $scope.good = new Object();
//		$scope.good.store = ''; //库存
//		$scope.good.price = ''; //价格
		//修改库存和价钱--这2个功能不要了
//		$scope.change_storeAndPrice = function() {
//			var good_ids = $scope.getCheckBoxValue();
//			//库存
//			Productmanager_sale_edit.change_store(good_ids, 'product_id', $scope.good.store, function(data) {
//				
//			});
//			//价格
//			Productmanager_sale_edit.change_price(good_ids, 'product_id', $scope.good.price, function(data) {
//			});
//		}

		$scope.init_data = function() {
			Productmanager_sale_edit.goods_search($scope.input.key, 1, 10, $scope.order, $scope.filterString, true,function(data) {
				$scope.goods = data.goods;
			});
			Productmanager_sale_edit.brand_list(function(data){
				$scope.brands = data.brand;
			});
		}
		$scope.init_data(); //必须在定义之后

		if($scope.productModel == undefined) $scope.productModel = new Object();
		//全選 數組
		$scope.productModel.danModel = new Array();
		$scope.productModel.is_all = false;
		
		/***分类三级联动***/
		$scope.input.cat1 = null;
		$scope.input.cat2 = null;
		$scope.input.cat3 = null;
		//下拉侦听
		$scope.listen_cat1 = function(data, obj) {
			$scope.input.cat2 = null;
			$scope.input.cat3 = null;
		};
		$scope.listen_cat2 = function(data, obj) {
			$scope.input.cat3 = null;
		};
		//定义绑定
		var items_cat = new Array();
		var obj1_cat = { name: 'input.cat1' };
		var obj2_cat = { name: 'input.cat2' };
		var obj3_cat = { name: 'input.cat3' };
		items_cat.push(obj1_cat);
		items_cat.push(obj2_cat);
		items_cat.push(obj3_cat);
		GoCatTypeAuth.init_load_only($scope, items_cat);

		//筛选的内容
		$scope.init_filter = function(){
            $scope.input.key = "";
            $scope.filter = new Object();
            $scope.filter.bn = "";//商品编码
            $scope.filter.brand_id = new Array();//品牌数组--本来是3个的，现在客户只要一个
            $scope.filter.cat_id = new Array();//品类数组
            $scope.filter.price_from = "";
            $scope.filter.price_to = "";
            $scope.filter.custom_cat_id = "";//店铺自定义分类ID
            $scope.filter.goods_state = "";
        }
        $scope.init_filter();//初始化搜索条件

        $scope.getType = function($event,value){
            var child = $event.target;
			for (var i=0;i<child.parentNode.children.length;i++) {
				child.parentNode.children[i].className = "col font-large-my";
			}
			//取消选中
			if($scope.filter.goods_state == value){
				$scope.filter.goods_state = "";
			}else{
				$scope.filter.goods_state = value;
				child.className = "col assertive font-large-my";
			}
        }

        //重置
        $scope.reset = function(){
            $scope.init_filter();//初始化搜索条件
            $scope.input.cat1 = null;
            $scope.input.cat2 = null;
            $scope.input.cat3 = null;
        }
        //删除input.key
        $scope.initKey = function(){
        	$scope.input.key = "";
        }
        //筛选
        $scope.search = function(){
            if($scope.input.cat1==null) $scope.filter.cat_id[0]="";
			if($scope.input.cat2==null) $scope.filter.cat_id[1]="";
			if($scope.input.cat3==null) $scope.filter.cat_id[2]="";
			if($scope.input.cat1) $scope.filter.cat_id[0]= $scope.input.cat1.id;
			if($scope.input.cat2) $scope.filter.cat_id[1]= $scope.input.cat2.id;
			if($scope.input.cat3) $scope.filter.cat_id[2]= $scope.input.cat3.id;

            $scope.filterString = JSON.stringify($scope.filter);
            $scope.doRefresh();
            $scope.closeRightMenu();
        }
        
        /*===========刷新====================*/
     	$scope.nPage = 1;
		$scope.isLoad = false; //可以加载
		//下拉刷新 判断哪个模块进行刷新
		$scope.doRefresh = function() {
				$scope.isLoad = true;
				Productmanager_sale_edit.goods_search($scope.input.key, 1,10, $scope.order, $scope.filterString, true, function(data) {
					if(data!=null){
						$scope.goods = data.goods;
					}else{
						$scope.goods = new Object();
					}
					$scope.nPage = 1;
				},function() {
					$scope.$broadcast('scroll.refreshComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);
				});
		};
		//加载更多
		$scope.doRefreshMore = function() {
				$scope.isLoad = true;
				Productmanager_sale_edit.goods_search($scope.input.key, $scope.nPage+1,10, $scope.order, $scope.filterString, true, function(data) {
					if(data.goods!=null){
						if($scope.goods!=undefined){
							$scope.goods = $scope.goods.concat(data.goods);
							$scope.nPage += 1;
						}
						else{
							$scope.goods = data.goods;
						}
					}
				},function() {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);
				});
		};	   


	}

	controller.$inject = ['$scope', 'Productmanager_sale_edit', '$timeout', 'Auth', 'GoCatTypeAuth', '$ionicSideMenuDelegate', 'GoPage', 'GoDialog'];

	return app.registerController("productmanager_sale_editctrl",controller);
});